package org.shj.thread.basic;

import org.shj.thread.util.Util;

/**
 *
 * @author Shen Huang Jian
 * @date 2018-01-02 16:30:28
 */
public class ThreadState {

	public static void main(String[] args){
		new Thread(new TimeWaiting(), "TimeWaitingThread").start();
		new Thread(new Waiting(), "WaitingThread").start();
		
		//使用两个Blocked，使得一个运行，一个被阻塞
		new Thread(new Blocked(), "BlockedThread-1").start();
		new Thread(new Blocked(), "BlockedThread-2").start();
	}
	
	//此线程不断地睡眠
	static class TimeWaiting implements Runnable{
		public void run() {
			while(true){
				Util.sleep(100);
			}
		}
	}
	
	//此线程在Waiting.class上等待
	static class Waiting implements Runnable{
		public void run() {
			while(true){
				synchronized(Waiting.class){
					try{
						Waiting.class.wait();
					}catch(InterruptedException e){
						e.printStackTrace();
					}
				}
			}
		}
	}
	
	static class Blocked implements Runnable{
		public void run(){
			synchronized(Blocked.class){
				while(true){
					Util.sleep(100);
				}
			}
		}
	}
	
}
